<template>
  <tm-app ref="app">
    <!-- #ifndef H5 -->
    <tm-navbar hide-home>
      <template #left>
        <SpaceItem class="home-space-nav-item" @click="openSpaceList" />
      </template>
      <template #default>
        <view class="home-navbar-placeholder"></view>
      </template>
    </tm-navbar>
    <!-- #endif -->
    <headers title="首页" :show="isShow" @open="open" @close="close" />
    <tag
      :tagSize="'n'"
      :taglist="Tags"
      :tagActive="tagActive"
      @btnClick="changeData"
    />
    <view v-if="tagActive === 'workbench'" class="custom-home-card">
      <!-- <template v-if="proTalData.length > 0"> -->
      <card-template
        v-for="(item, index) in proTalData"
        :key="index"
        :title="item.title"
        :margin="[20, 20]"
        :padding="[20, 14]"
        :extra="item.extra"
        :list="item.info"
      />
      <!-- </template> -->
      <!-- <tm-sheet :margin="[0, 2]" v-else>
        <tm-result
          :showBtn="false"
          title="数据加载中"
          class="custom-work-result"
        ></tm-result>
      </tm-sheet> -->
      <!-- <asset-card-list
        :title="'个人资产台账'"
        :margin="[20, 20]"
        :padding="[20, 14]"
      /> -->
    </view>
    <view v-else class="custom-home-card"> 计划中，尽情期待 </view>
  </tm-app>
</template>

<script lang="ts" setup>
import { Tags } from './config/index'
// #ifdef H5
import { getCurrentInstance } from 'vue'
import { useRoute } from 'vue-router'
// #endif
import { onMounted, ref } from 'vue'
import { IPortal } from './common/portalType'
import cardTemplate from './components/cardTemplate.vue'
import tag from '../../components/tag/index.vue'
import headers from '../../components/header/header.vue'
import SpaceItem from '../../components/space/space_item.vue'
import porTal from './common/portal'
import { TabItems } from '@/ts/base/model'
import { onShow } from '@dcloudio/uni-app'
let proTalData = ref<IPortal[]>([])
let isShow = ref<boolean>(false)
const tagActive = ref<string>('workbench')
// import { onShow, onLoad, onInit } from '@dcloudio/uni-app'
// const { proxy } = getCurrentInstance()
/** 加载数据 */
const getData = async () => {
  uni.showLoading({
    title: '数据加载中'
  })

  await porTal.initPortal()
  proTalData.value = porTal.getPortal()
  uni.hideLoading()

  // try {
  //   await porTal.initPortal()
  //   proTalData.value = porTal.getPortal()
  //   uni.hideLoading()
  // } catch (error) {
  //   uni.hideLoading()
  //   //打印错误
  //   console.error('获取门户数据失败：'+ error)
  // }
}

async function changeData(item: TabItems) {
  tagActive.value = item.code
}

/** 打开弹窗 */
const open = () => {
  isShow.value = true
}

/** 关闭弹窗 */
const close = () => {
  isShow.value = false
}

const openSpaceList = (): void => {
  uni.navigateTo({
    url: '/pages/commons/space_list'
  })
}

onShow(() => {
  if (proTalData.value.length === 0) {
    getData()
  }
})

onMounted(() => {
  getData()
  // #ifdef H5
  const route = useRoute()
  const instance = getCurrentInstance()
  const queue = instance?.appContext.config.globalProperties.$queue
  if (queue) {
    queue.push({
      id: route.meta.id,
      title: route.meta.title,
      path: route.path
    })
  }
  // #endif
})
</script>
<style lang="scss">
.custom-home-card {
  height: calc(100vh - 370rpx);
  overflow-y: scroll;
}

.home-space-nav-item {
  max-width: 620rpx;
}
</style>
